package com.flats.webview;

import com.flats.model.entity.Address;
import com.flats.model.entity.Users;
import com.flats.service.transactional.TransactionalAddressService;
import com.flats.utils.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Set;
import java.util.TreeSet;

@Controller
public class MainController {

    @Autowired
    TransactionalAddressService addressService;

    @RequestMapping(value = "/main")
    public String getMainView(Model model) {
        Users currentUser = getParamFromSession(Constants.USER);
        if (currentUser != null) {
            model.addAttribute("user", currentUser);
        }
        Set<String> countries = new TreeSet<String>();
        for (Address address : addressService.getAll()) {
            countries.add(address.getCountry());
        }
        setParamToSession(Constants.COUNTRIES, countries);
        model.addAttribute("countryList", countries);
        return "main";
    }

    protected void setParamToSession(String paramName, Object paramValue) {
        Utils.setSession(paramName, paramValue);
    }

    protected Users getParamFromSession(String param) {
        return (Users) Utils.getSession(param);
    }



}
